package com.sky.service.impl;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppinCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppinCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断当前购物车是否存在该商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long id = BaseContext.getCurrentId();
        //设置用户id
        shoppingCart.setUserId(id);
        List<ShoppingCart> list = shoppingCartMapper.get(shoppingCart);

        //如果存在，数量+1
        if (list != null && list.size() > 0) {
            //获得购物车中存在的商品
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart1);
        } else {
            //如果不存在，添加到购物车,判断是套餐，还是菜品
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
            } else {
                Long setmealId = shoppingCart.getSetmealId();
                SetmealVO setmeal = setmealMapper.getSetmealById(setmealId);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //将菜品或套餐插入购物车
            shoppingCartMapper.inset(shoppingCart);

        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.list(userId);
    }

    /**
     * 删除购物车中的一个
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.get(shoppingCart);
        if(shoppingCarts ==null || shoppingCarts.size()==0){
            throw new DeletionNotAllowedException(MessageConstant.UNKNOWN_ERROR);
        }
        ShoppingCart cart = shoppingCarts.get(0);
        //判断数量是否大于1
        Integer number = cart.getNumber();
        if(number==1){
           // 商品的数量=1，直接删除
            shoppingCartMapper.delete(shoppingCart);
        }else{
            //商品数量大于一， 数量减1
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.deleteOne(cart);
        }


    }

    /**
     * 清空购物车
     * @param shoppingCart
     */

    @Override
    public void clean(ShoppingCart shoppingCart) {
        shoppingCartMapper.clean(shoppingCart);
    }


}

